import { Channel, ChannelBase, Sharelink } from '@/types/share-link';
import apiClient from '../apiClient';
import { PaginationProps, PaginationResponse } from '@/common/types';

export enum SharelinkApi {
  /**
   * 分享链接
   */
  Sharelink = '/openapi/v1/sharelink',
  SharelinkStatus = '/openapi/v1/sharelink/{id}/status',
  SharelinkLogin = '/openapi/v1/user/login/guest',

  /**
   * 发布渠道
   */
  ChannelList = '/openapi/v2/channels/list',
  GetChannelInfo = '/openapi/v2/channels/info',
  CreateChannel = '/openapi/v2/channels/create',
  UpdateChannel = '/openapi/v2/channels/update',
  DeleteChannel = '/openapi/v2/channels/delete',
}

interface SharelinkReq {
  page?: number;
  page_size?: number;
  keywords?: string;
  app_id?: string;
}

interface SharelinkResponse {
  list: Sharelink[];
  total: number;
  page: number;
  page_size: number;
}

/**
 * 获取分享链接列表
 * @returns 分享链接列表
 */
const getShareLinkList = (params: SharelinkReq) =>
  apiClient.get<SharelinkResponse>({ url: SharelinkApi.Sharelink, params });

/**
 * 创建分享链接
 * @returns 结果
 */
const createShareLink = (data: Sharelink) => apiClient.post({ url: SharelinkApi.Sharelink, data });

/**
 * 更新分享链接
 * @returns 结果
 */
const updateShareLink = (id: string, data: Sharelink) =>
  apiClient.put({ url: SharelinkApi.Sharelink + `/${id}`, data });

/**
 * 删除分享链接
 * @returns 结果
 */
const deleteShareLink = (id: string) =>
  apiClient.delete({ url: SharelinkApi.Sharelink + `/${id}` });

/**
 * 更新分享链接状态
 * @returns 结果
 */
const updateShareLinkStatus = (id: string) =>
  apiClient.post({ url: SharelinkApi.SharelinkStatus.replace('{id}', id) });

/**
 * 匿名登录
 * @returns 结果
 */
const anonymousLogin = ({ share_code, device_code }: { share_code: string; device_code: string }) =>
  apiClient.post({
    url: SharelinkApi.SharelinkLogin,
    data: { share_code, device_code },
  });

/**
 * 获取发布渠道列表
 * @param params 分页参数
 * @returns 发布渠道列表
 */
const getChannelList = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<Channel>>({ url: SharelinkApi.ChannelList, params });

/**
 * 创建发布渠道
 * @param data 发布渠道信息
 * @returns 结果
 */
const createChannel = (data: ChannelBase) =>
  apiClient.post({ url: SharelinkApi.CreateChannel, data });

/**
 * 更新发布渠道
 * @param id 发布渠道ID
 * @param data 发布渠道信息
 * @returns 结果
 */
const updateChannel = (data: ChannelBase) =>
  apiClient.post({ url: SharelinkApi.UpdateChannel, data });

/**
 * 删除发布渠道
 * @param id 发布渠道ID
 * @returns 结果
 */
const deleteChannel = (id: string) =>
  apiClient.post({ url: SharelinkApi.DeleteChannel, params: { id } });

export default {
  getShareLinkList,
  createShareLink,
  updateShareLink,
  deleteShareLink,
  updateShareLinkStatus,
  anonymousLogin,
  getChannelList,
  createChannel,
  updateChannel,
  deleteChannel,
};
